/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.client.view;

import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;
import java.awt.*;

public class ScrollBarPanel extends JPanel {

    private JScrollBar scrollBar = null;
    final ChartView chart;
    JPanel panel = null;
    AdjustmentListener adjuster = null;

    public void fireAdjustmentEvent() {
        try {
            if (adjuster != null) {
                adjuster.adjustmentValueChanged(null);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public ScrollBarPanel(final ChartView chart) {
        //super("", new BorderLayout(0,0));
        panel = new JPanel();
        this.chart = chart;
        JPanel titlebar = new JPanel();
        JPanel cmbPanel = new JPanel();
        JPanel footer = new JPanel();
        titlebar.setLayout(new BorderLayout(0, 25));
        titlebar.add(cmbPanel, BorderLayout.NORTH);
        footer.setLayout(new BorderLayout(0, 25));
        scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
        chart.setScrollBar(scrollBar);

        footer.add(panel, BorderLayout.NORTH);

        AdjustmentListener scrollBarAdjuster = new AdjustmentListener() {

            public void adjustmentValueChanged(AdjustmentEvent ae) {
                try {
                    ChartView theChart = chart;
                    int vsz = theChart.getScrollBarMaximum();
                    scrollBar.setMaximum(vsz);
                    scrollBar.setMinimum(0);
                    if (theChart != null) {
                        if (ae != null) {
                            int v = ae.getValue();
                            theChart.scrollTo(v);
                        }
                        scrollBar.repaint();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };

        adjuster = scrollBarAdjuster;

        scrollBar.addAdjustmentListener(scrollBarAdjuster);


        panel.setLayout(new BorderLayout());
        panel.add(scrollBar, BorderLayout.CENTER);
        setLayout(new BorderLayout());
        add(chart, BorderLayout.CENTER);
        add(panel, BorderLayout.SOUTH);
    }

    public JScrollBar getScrollBar() {
        return scrollBar;
    }
}
